package com.lk.org.controller;

import com.lk.org.service.IShopService;
import com.lk.org.domain.Shop;
import com.lk.org.query.ShopQuery;
import com.lk.basic.util.AjaxResult;
import com.lk.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    @PutMapping
    public AjaxResult save(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.insert(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    @PutMapping("/examine")
    public AjaxResult examine(@RequestBody Shop shop){
        try {
            shopService.examine(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delById(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selById(@PathVariable("id")Long id)
    {
        try {
            Shop shop = shopService.selById(id);
            return AjaxResult.me().setResult(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    @GetMapping
    public AjaxResult selAll(){

        try {
            List< Shop> list = shopService.selAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }

    @PostMapping
    public AjaxResult pageList(@RequestBody ShopQuery query) {
        try {
            PageList<Shop> pageList = shopService.queryPage(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
    @GetMapping("/active2/{id}")
    public AjaxResult active1(@PathVariable("id") Long id) {
        try {
            shopService.active(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("激活失败！"+e.getMessage());
        }
    }
}
